/**


    @file This program makes the sorting of
          poem "Evgeny Onegin" in alphabetical order

    @author Nikita Berenkov, student of 1st course DREC MIPT.
    @version 1.1.0
    @mainpage
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<locale.h>
#include<assert.h>

const int MAXLINES =10000;

//! @brief This function defines whether the first line is bigger or not
/// @param s1 - the first line
/// @param s2 - the second line

int  my_strcmp (char *s1, char*s2);

//! @brief This function finds the line's length
/// @param s - the line

size_t  my_strlen( char*s);

//! @brief this function calculates the file length
/// @param t - the massive of points to the lines

long  counting_Lines(char* t);



//! @brief This is my own sorting function
/// @param text[] - massive of lines
/// @param left,right - the borders of sorting

void  qsortic ( char *text[],int left ,int right);

//! @brief This  function is swapping the lines
/// @param i,j - numbers of swapped elements

void  swapp( char *text[] ,int i,int j);


//long const MAXLINES = 10000;

int main(void)
{
    char *text[MAXLINES];

    setlocale(LC_CTYPE, "Rus");

    long int nLines;

    FILE *fp;

    if((fp = fopen("input.txt", "rb")) == NULL)
    {
     printf ("Cannot open file.\n");
     exit(1);
    }


    fseek(fp,0,SEEK_END);

    long fSize = ftell(fp);

    printf("%d\n",fSize);

    fseek(fp,0,SEEK_SET);

    char* buffer = (char*) calloc(fSize, sizeof(*buffer));

    assert(buffer);

    if (fread (buffer, sizeof(*buffer), fSize, fp) > fSize)

    assert(("file read failed!", 0));

  // for(int i= 0 ;i<11000;i++)
   // printf("%c",buffer[i]);

    fclose(fp);  fp = NULL;

    nLines = counting_Lines(buffer);


    //char** text = (char**) calloc(*nLines + 1, sizeof(*text));
    //assert(txt);

    text[0] = buffer;
    int i = 1;

    for(char* buf_ptr = buffer + 1; *buf_ptr; buf_ptr++)
    {
        if (*(buf_ptr - 1) == '\n')
        {
            text[i] = buf_ptr;
            // printf("%s\n",text[i]);
            i++;
            *(buf_ptr - 1) = '\0';
        }
    }
    free(buffer);

    qsortic(text,0,nLines);

    FILE *fpout;


    if ((fpout = freopen("output.txt","w",stdout)) == NULL)

    {
        printf("can't open\n");
        exit(1);
    }
//printf("%d\n",nLines);

        for (int i = 0;i < nLines;i++)
        printf("%s\n",text[i]);

        fclose(fpout);
    //free(text);

}


long int  counting_Lines(char* t)
{


    long int amount = 0;

    for (char* ptr = t; *ptr; ptr++)
    {
        if (*ptr == '\n')   amount++;
        if ((*ptr != '\n') && (*(ptr+1) == '\0'))   amount++;
    }



    return amount;
}



 void qsortic (  char *text[], int left, int right )
    {

    int i, last;
    //printf("%d %d\n",left,right);

    if (left >= right) return;

   swapp(text, left, (left + right)/2 );
    last = left;
   // printf("Printing here the left last and right %d %d %d\n",left,last,right);

    for(i = left + 1;i < right; i++)
        if (my_strcmp(( char*)text[i],(char*)text[left]) > 0)
        {
           // printf("%s %d %s %d\n",a[left].name,a[left].mark,a[i].name,a[i].mark);
            swapp( text, ++left, i);
           // printf("writing the left %d\n",left);
        }

    swapp(text, left, last );
   // printf("i nave changed the last and left %d %d\n",left,last);
    qsortic( text, left, last - 1);
    qsortic( text, last + 1, right);

    }





    void swapp(char *text[],int num1,int num2)
    {

      char *f;
      f = text[num1];
      text[num1] = text[num2];
      text[num2] = f;

    }


 int  my_strcmp (  char *s1,  char*s2)
 {
     int l = 0;
     int k = strlen(s1);
     int m = strlen(s2);

     if (m > k) l = m;
     else l = k;

     for (int i=0;i<l;i++)
     {
        if (s1[i] < s2[i])
      {
        return 1;

      }

      else if (s1[i] > s2[i])
      return -1;

 }
 return 0;

 }


 size_t  my_strlen( char*s)
 {
     size_t k=0;
     int i=0;
     while (s[i]!='\0')
     {


     k++;
     i++;
     }
return k;
 }
